package cn.itcast.ks.service.impl;

import cn.itcast.ks.common.AppHttpCodeEnum;
import cn.itcast.ks.common.ResponseResult;
import cn.itcast.ks.common.threadlocal.UserThreadLocalUtils;
import cn.itcast.ks.common.utils.AppJwtUtil;
import cn.itcast.ks.mapper.ClassMapper;
import cn.itcast.ks.mapper.UserMapper;
import cn.itcast.ks.pojo.dtos.AddIntegralDto;
import cn.itcast.ks.pojo.dtos.UserDto;
import cn.itcast.ks.pojo.entity.User;
import cn.itcast.ks.service.IUserService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mmff
 * @since 2021-05-03
 */
@Slf4j
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    //根据班级ID查询所有的学生
    @Override
    public ResponseResult findByClassId() {
        //todo 多种方法,记得尝试
        //todo 传出其实是vo，有时间改
        //获得用户存在header的ClassId  方法一
        User user = UserThreadLocalUtils.getUser();
        //从token中获得classid 方法二
        //String token = httpServletRequest.getHeader("token");
//        Claims claimsBody = AppJwtUtil.getClaimsBody(token);
//        System.out.println("claimsBody" + claimsBody);
        /*
        userinfo={"classId":1,"createdOn":1619971200000,"id":1,"integral":2,"isDeleted":"0",
        "mobile":"13012345678","name":"张三","password":"","roleId":1,"salt":"","status":"1"}
                 */
//        User userinfo = (User) claimsBody.get("userinfo");
//        System.out.println("userinfo"+userinfo);
//        Integer classId = userinfo.getClassId();
        // 1. 获得用户存在header的ClassId
        // 1.1 根据ClassId查询班级学生信息
        Integer classId = user.getClassId();
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getClassId, classId).eq(User::getRoleId, 2);
        // 1.2 获取班级信息集合
        List<User> users = this.list(wrapper);
        // 将User集合对象转换为UserDto集合对象
        List<UserDto> userDtos = JSON.parseArray(JSON.toJSONString(users), UserDto.class);
        // 循环遍历 设置小组成员总分数 方法一
        for (UserDto userDto : userDtos) {
            Integer groupsIntegral = userMapper.findByClassId(userDto.getGroupId());
            userDto.setGroupsIntegral(groupsIntegral);
        }

        //获得小组总分方法二
//        Integer[] groupsIntegral = userMapper.getGroupsIntegrl();
//        Integer[] groupIds = userMapper.getGroupIds();
//        System.out.println(groupsIntegral);//[5, 56]
//        System.out.println(groupIds);//[12, 13]

//        userDtos = userDtos.stream().map(c -> {
//            for (int i = 0; i < groupIds.length; i++) {
//                if (c.getGroupId().equals(groupIds[i])) {
//                    c.setGroupsIntegral(groupsIntegral[i]);
//                }
//            }
//            return c;
//        }).collect(Collectors.toList());

        // 返回执行方法结果
        System.out.println(userDtos);
        return ResponseResult.okResult(userDtos);
    }

    //为用户添加积分
    @Override
    public ResponseResult addIntegral(AddIntegralDto dto) {
        // 1.修改学生分值数据
        User user = this.getById(dto.getId());
        user.setIntegral(user.getIntegral() + dto.getIntegral());
        // 2. 累加分数 修该分值
        this.updateById(user);
        return ResponseResult.okResult("添加积分成功");
    }

    //重置小组积分
    @Override
    public ResponseResult resetGScoreByGroupId(Integer classId) {

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getClassId, classId);
        List<User> users = this.list(wrapper);
        users = users.stream().map(c -> {
            c.setIntegral(0);
            return c;
        }).collect(Collectors.toList());
        this.updateBatchById(users);
        return ResponseResult.okResult("重置成功");
    }

}
